package com.jcfy.project.module.controller;

import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jcfy.framework.aspectj.lang.annotation.Log;
import com.jcfy.framework.aspectj.lang.enums.BusinessType;
import com.jcfy.project.module.domain.TUser;
import com.jcfy.project.module.service.ITUserService;
import com.jcfy.framework.web.controller.BaseController;
import com.jcfy.framework.web.domain.AjaxResult;
import com.jcfy.common.utils.poi.ExcelUtil;
import com.jcfy.framework.web.page.TableDataInfo;

/**
 * 用户Controller
 *
 * @author jiangfeng
 * @date 2020-08-05
 */
@RestController
@RequestMapping("/module/user" )
public class TUserController extends BaseController {
    @Autowired
    private ITUserService tUserService;

    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('module:user:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(TUser tUser) {
        startPage();
        List<TUser> list = tUserService.selectTUserList(tUser);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @PreAuthorize("@ss.hasPermi('module:user:export')" )
    @Log(title = "用户" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(TUser tUser) {
        List<TUser> list = tUserService.selectTUserList(tUser);
        ExcelUtil<TUser> util = new ExcelUtil<TUser>(TUser. class);
        return util.exportExcel(list, "user" );
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:user:query')" )
    @GetMapping(value = "/{userId}" )
    public AjaxResult getInfo(@PathVariable("userId" ) Long userId) {
        return AjaxResult.success(tUserService.selectTUserById(userId));
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('module:user:add')" )
    @Log(title = "用户" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TUser tUser) {
        return toAjax(tUserService.insertTUser(tUser));
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('module:user:edit')" )
    @Log(title = "用户" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TUser tUser) {
        return toAjax(tUserService.updateTUser(tUser));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('module:user:remove')" )
    @Log(title = "用户" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}" )
    public AjaxResult remove(@PathVariable Long[] userIds) {
        return toAjax(tUserService.deleteTUserByIds(userIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('module:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TUser tUser) {
        return toAjax(tUserService.updateTUser(tUser));
    }
}
